<template>
	<view class="my-settle-container">
		<!-- 全选 -->
		<view class="chooseAll">
			<label class="radio">
				<radio :checked="isFullCheck" color="#d1372e" @click="count" /><text> 全选</text>
			</label>
		</view>
		<!-- 合计 -->
		<view class="amount-box">
			合计：<text class="amount">￥{{countTotalMoney}}</text>
		</view>
		<!-- 结算 -->
		<view class="btn-settle" @click="settlement">
			结算(<text>{{total}}</text>)
		</view>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		name: "my-settle",
		data() {
			return {
				seconds: 3
			};
		},
		computed: {
			...mapGetters('my_cart', ['checkCount', 'total', 'countTotalMoney']),
			...mapState('my_cart', ['cart']),
			...mapState('my_user', ['token']),
			...mapGetters('my_user', ['addStr']),
			isFullCheck() {
				return this.total === this.checkCount
			}
		},
		methods: {
			...mapMutations('my_cart', ['updateAllGoodsState']),
			...mapMutations('my_user', ['updateRedirectinfo']),
			count() {
				this.updateAllGoodsState(!this.isFullCheck)
			},
			settlement() {
				//没选择商品
				if (!this.checkCount) return uni.$showMsg('请选择要结算的商品！')
				//没填收货地址
				if (!this.addStr) return uni.$showMsg('请选择收获地址！')
				//未登录
				if (!this.token) return this.delayNavigator()
				this.payOrder()
			},
			//页面三秒之后跳转
			delayNavigator() {
				this.showTips(this.seconds)
				let timeId = setInterval(() => {
					this.seconds--
					if (this.seconds <= 0) {
						clearInterval(timeId)
						uni.switchTab({
							url: '/pages/my/my',
							success: () => {
								this.updateRedirectinfo({
									openType: 'switchTab',
									from: 'pages/cart/cart'
								})
							}
						})
						return
					}
					this.showTips(this.seconds)
				}, 1000)
			},
			// 支付订单
			async payOrder() {
				const orderInfo = {
					orderPrice: 0.01,
					consignee_addr: this.addStr,
					goods: this.cart.filter(x => ({
						goods_id: x.goods_id,
						goods_count: x.goods_count,
						goods_pirce: x.goods_pirce,
						goods_number: x.goods_number
					}))
				}
				const {
					data: res
				} = await uni.$http.post('/api/public/v1/my/orders/create', orderInfo)
				console.log(res)
				// if (res.meta.status !== 200) return uni.$showMsg('创建订单失败！')
				const payInfo = res.message
				const res2 = uni.requestPayment(payInfo)
				if (res2) uni.$showMsg('订单未支付')
			},
			//提示信息页面
			showTips(n) {
				uni.showToast({
					icon: 'none',
					title: '请登录后再结算' + n + '秒后自动跳转到登陆页面',
					duration: 1500,
					mask: true
				})
			}
		}

	}
</script>

<style lang="scss">
	.my-settle-container {
		display: flex;
		justify-content: space-between;
		align-items: center;
		position: fixed;
		bottom: 0;
		left: 0;
		height: 50px;
		width: 100%;
		background-color: white;
		font-size: 14px;
		padding-left: 5px;

		.chooseAll {
			display: flex;
			align-items: center;
		}

		.amount-box {
			.amount {
				color: #ee2e07;
				font-weight: bold;

			}

		}

		.btn-settle {
			height: 45px;
			width: 100px;
			line-height: 45px;
			text-align: center;
			background-color: #ee2e07;
			color: white;
		}
	}
</style>